<?php
namespace Anls\Com\Strategy;
use Anls\Com\Indicator;
/**
 * 条件
 * @author w.
 *
 */
class Condition {
   
    // 开仓条件
	private $openCdts = array();
	
	// 平仓条件
	private $closeCdts = array();
	
	public function __construct(){
	}
	
	/**
	 * 检查条件
	 * 
	 * @param array $cdts
	 * @param array $bar
	 * @param int $shift
	 */
	private function checkCondition($cdts,$bar,$shift){
	    $rst = false;
	    if(count($cdts) == 0) return true;
	    foreach ($cdts as $cdt){
	        if($cdt['exprType'] == Expr::EXPR_BEB_TYPE){
	            $BA = $cdt['BA'];
	            $E = $cdt['E'];
	            $BB = $cdt['BB'];
	            if($E == '&gt;'){
	                // 大于
	               
	                if($bar[$shift][$BA] > $bar[$shift][$BB]){
	                    $rst = true;
	                    continue;
	                } else {
	                    return false;
	                }
	            }
	            if($E == '&gt;='){
	                // 大于等于
	                if($bar[$shift][$BA] >= $bar[$shift][$BB]){
	                    $rst = true;
	                    continue;
	                } else {
	                    return false;
	                }
	            }
	            if($E == '&lt;'){
	                // 小于
	                if($bar[$shift][$BA] < $bar[$shift][$BB]){
	                    $rst = true;
	                    continue;
	                } else {
	                    return false;
	                }
	            }
	            if($E == '&lt;='){
	                // 小于等于
	                if($bar[$shift][$BA] <= $bar[$shift][$BB]){
	                    $rst = true;
	                    continue;
	                } else {
	                    return false;
	                }
	            }
	            if($E == 'cross'){
	                // 上穿
	                if(Indicator\Shape::crossOver($bar, $BA, $BB, $shift)){
	                    $rst = true;
	                    continue;
	                } else {
	                    return false;
	                }
	            }
	        }
	    }
	    return $rst;
	}
	
	/**
	 * 检查开仓条件
	 * 
	 * @param array $bar
	 * @param int $shift
	 */
	public function checkOpen($bar,$shift){
	    return $this->checkCondition($this->openCdts, $bar, $shift);
	}
	
	/**
	 * 检查平仓条件
	 * 
	 * @param array $bar
	 * @param int $shift
	 */
	public function checkClose($bar,$shift){
	    return $this->checkCondition($this->closeCdts, $bar, $shift);
	}
	
    public function setOpenCdtsByExpr($expr){
        $_andOr = explode(',',$expr);
        
        $this->openCdts = $this->getCdtsByExpr($expr);
    }
    
    public function setCloseCdtsByExpr($expr){
        $this->closeCdts = $this->getCdtsByExpr($expr);
    }
    
    /**
     * 根据表示式获取条件
     * @param string $expr
     */
    private function getCdtsByExpr($expr){
        $rst = array();
        $and = explode(",", $expr);
        foreach ($and as $cdt){
            $exprType = Expr::getExprType($cdt);
            if($exprType == Expr::EXPR_BEB_TYPE){
                // BEB类型
                $B = Expr::machKey(Expr::B_REGEX, $cdt)[1];
                $E = Expr::machKey(Expr::E_REGEX, $cdt)[1];
                $_cdt = array(
                    'exprType' => $exprType
                    ,'BA' => $B[0]
                    ,'E' => $E[0]
                    ,'BB' => $B[1]
                );
                array_push($rst, $_cdt);
            }
        }
        return $rst;
    }
}

?>
